﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TjZheng.Framework;
using TjZheng.News.DataEntity;
using TjZheng.Cache;
using TjZheng.News.Business;
using System.Web;

namespace TjZheng.News.Process
{
    public partial class ArticleInfoProcess
    {
        /// <summary>
        /// 获取版块文章列表
        /// </summary>
        /// <param name="categoryId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="isLoadArticleContent"></param>
        /// <param name="isLoadPictureCollection"></param>
        /// <param name="isConvertTargetUrl"></param>
        /// <returns></returns>
        public virtual InvokeResult GetArticleInfoList(int? categoryId, int? pageIndex = 1, int? pageSize = 10, int? isLoadArticleContent = 0, int? isLoadPictureCollection = 0, int? isConvertTargetUrl = 1)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (!categoryId.HasValue)
            {
                objInvokeResult.ResultCode = "CategoryIdEmpty";
                objInvokeResult.ResultMessage = "CategoryId不存在";
                return objInvokeResult;
            }
            if (!this.ExistTableArticleInfo(categoryId.Value))
            {
                objInvokeResult.ResultCode = "TableEmpty";
                objInvokeResult.ResultMessage = "文章表不存在";
                return objInvokeResult;
            }
            if (!pageIndex.HasValue || pageIndex <= 0)
            {
                pageIndex = 1;
            }
            if (!pageSize.HasValue || pageSize <= 0 || pageSize > 20)
            {
                pageSize = 10;
            }
            BizArticleInfo objBizArticleInfo = new BizArticleInfo(categoryId.Value.ToString());
            List<ArticleInfo> objArticleInfoList = objBizArticleInfo.GetPageListCache("", TimeSpan.FromMinutes(1), pageIndex.Value - 1, pageSize.Value, "IsRelease=1", "PublishDate desc");
            if (objArticleInfoList.IsNull())
            {
                objInvokeResult.ResultCode = "ArticleInfoNull";
                objInvokeResult.ResultMessage = "没有更多文章了";
                return objInvokeResult;
            }
            bool objLoadArticleContent = isLoadArticleContent.HasValue && isLoadArticleContent.Value == 1;
            bool objLoadPictureCollection = isLoadPictureCollection.HasValue && isLoadPictureCollection.Value == 1;
            List<ArticleResultInfo> objArticleResultInfoList = objArticleInfoList.CreateArticleResultInfoList(objLoadArticleContent, objLoadPictureCollection);

            bool objConvertTargetUrl = isConvertTargetUrl.HasValue && isConvertTargetUrl.Value == 1;
            if (objConvertTargetUrl)
            {
                objArticleResultInfoList.ConvertTargetUrl(null, this.ArticlePlatformType);
            }
            objInvokeResult.Data = objArticleResultInfoList;
            return objInvokeResult;
        }
        /// <summary>
        /// 获取版块文章列表
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="isLoadArticleContent"></param>
        /// <param name="isLoadPictureCollection"></param>
        /// <param name="isConvertTargetUrl"></param>
        /// <returns></returns>
        public virtual InvokeResult GetArticleCollectList(int? parentId, int? pageIndex = 1, int? pageSize = 10, int? isLoadArticleContent = 0, int? isLoadPictureCollection = 0, int? isConvertTargetUrl = 1)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (!parentId.HasValue)
            {
                objInvokeResult.ResultCode = "ParentIdEmpty";
                objInvokeResult.ResultMessage = "ParentId不存在";
                return objInvokeResult;
            }
            if (!pageIndex.HasValue || pageIndex <= 0)
            {
                pageIndex = 1;
            }
            if (!pageSize.HasValue || pageSize <= 0 || pageSize > 20)
            {
                pageSize = 10;
            }
            BizArticleCollect objBizArticleCollect = new BizArticleCollect(parentId.Value.ToString());
            List<ArticleCollect> objArticleCollectList = objBizArticleCollect.GetPageListCache("", TimeSpan.FromMinutes(1), pageIndex.Value - 1, pageSize.Value, "", "PublishDate desc");
            if (objArticleCollectList.IsNull())
            {
                objInvokeResult.ResultCode = "ArticleCollectNull";
                objInvokeResult.ResultMessage = "没有更多文章了";
                return objInvokeResult;
            }
            bool objLoadArticleContent = isLoadArticleContent.HasValue && isLoadArticleContent.Value == 1;
            bool objLoadPictureCollection = isLoadPictureCollection.HasValue && isLoadPictureCollection.Value == 1;
            List<ArticleResultInfo> objArticleResultInfoList = objArticleCollectList.CreateArticleResultInfoList(objLoadArticleContent, objLoadPictureCollection);

            bool objConvertTargetUrl = isConvertTargetUrl.HasValue && isConvertTargetUrl.Value == 1;
            if (objConvertTargetUrl)
            {
                objArticleResultInfoList.ConvertTargetUrl(null, this.ArticlePlatformType);
            }
            objInvokeResult.Data = objArticleCollectList;
            return objInvokeResult;
        }

        /// <summary>
        /// 搜索文章列表
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="keyword">搜索关键字</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="isLoadArticleContent"></param>
        /// <param name="isLoadPictureCollection"></param>
        /// <param name="isConvertTargetUrl"></param>
        /// <returns></returns>
        public virtual InvokeResult GetSearchArticleCollectList(int? parentId, string keyword, int? pageIndex = 1, int? pageSize = 10, int? isLoadArticleContent = 0, int? isLoadPictureCollection = 0, int? isConvertTargetUrl = 1)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (keyword.IsNull())
            {
                objInvokeResult.ResultCode = "KeywordNull";
                objInvokeResult.ResultMessage = "搜索关键字为空";
                return objInvokeResult;
            }
            if (!pageIndex.HasValue || pageIndex <= 0)
            {
                pageIndex = 1;
            }
            if (!pageSize.HasValue || pageSize <= 0 || pageSize > 20)
            {
                pageSize = 10;
            }
            BizArticleCollect objBizArticleCollect = new BizArticleCollect(parentId.Value.ToString());
            if (parentId.HasValue)
            {
                objBizArticleCollect = new BizArticleCollect(parentId.Value.ToString());
            }
            else
            {
                objBizArticleCollect = new BizArticleCollect();
            }
            List<ArticleCollect> objArticleCollectList = objBizArticleCollect.GetPageListCache("", TimeSpan.FromMinutes(1), pageIndex.Value - 1, pageSize.Value, "IsRelease=1 AND (ArticleTag like @KeyWord OR ArticleTitle like @KeyWord)", new { KeyWord = string.Format("%{0}%", keyword.FilterSql()) }, "PublishDate desc");
            if (objArticleCollectList.IsNull())
            {
                objInvokeResult.ResultCode = "ArticleCollectNull";
                objInvokeResult.ResultMessage = "没有更多文章了";
                return objInvokeResult;
            }

            bool objLoadArticleContent = isLoadArticleContent.HasValue && isLoadArticleContent.Value == 1;
            bool objLoadPictureCollection = isLoadPictureCollection.HasValue && isLoadPictureCollection.Value == 1;
            List<ArticleResultInfo> objArticleResultInfoList = objArticleCollectList.CreateArticleResultInfoList(objLoadArticleContent, objLoadPictureCollection);

            bool objConvertTargetUrl = isConvertTargetUrl.HasValue && isConvertTargetUrl.Value == 1;
            if (objConvertTargetUrl)
            {
                objArticleResultInfoList.ConvertTargetUrl(null, this.ArticlePlatformType);
            }
            objInvokeResult.Data = objArticleResultInfoList;
            return objInvokeResult;
        }

        /// <summary>
        /// 获取文章详情
        /// </summary>
        /// <param name="categoryId"></param>
        /// <param name="articleId"></param>
        /// <param name="publishDate"></param>
        /// <param name="isLoadArticleContent"></param>
        /// <param name="isLoadPictureCollection"></param>
        /// <param name="isConvertTargetUrl"></param>
        /// <param name="isPictureThumbnail"></param>
        /// <param name="isContentThumbnail"></param>
        /// <returns></returns>
        public virtual InvokeResult GetArticleInfo(int? categoryId, int? articleId, string publishDate, int? isLoadArticleContent = 0, int? isLoadPictureCollection = 0, int? isConvertTargetUrl = 1, int? isPictureThumbnail = 0, int? isContentThumbnail = 0)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (!categoryId.HasValue)
            {
                objInvokeResult.ResultCode = "CategoryIdEmpty";
                objInvokeResult.ResultMessage = "CategoryId不存在";
                return objInvokeResult;
            }
            if (!articleId.HasValue)
            {
                objInvokeResult.ResultCode = "IDEmpty";
                objInvokeResult.ResultMessage = "ArticleId不存在";
                return objInvokeResult;
            }
            if (publishDate.IsNull())
            {
                objInvokeResult.ResultCode = "publishDateEmpty";
                objInvokeResult.ResultMessage = "publishDate不存在";
                return objInvokeResult;
            }
            ArticleInfo objArticleInfo = this.GetArticleInfo(categoryId.Value, articleId.Value);
            if (objArticleInfo == null)
            {
                objInvokeResult.ResultCode = "ArticleInfoNull";
                objInvokeResult.ResultMessage = "文章不存在";
                return objInvokeResult;
            }
            if (!string.Equals(objArticleInfo.PublishDate.ToString("yyyyMMdd"), publishDate))
            {
                objInvokeResult.ResultCode = "PublishDateNotMatch";
                objInvokeResult.ResultMessage = "PublishDate不匹配";
                return objInvokeResult;
            }

            bool objLoadArticleContent = isLoadArticleContent.HasValue && isLoadArticleContent.Value == 1;
            bool objLoadPictureCollection = isLoadPictureCollection.HasValue && isLoadPictureCollection.Value == 1;
            ArticleResultInfo objArticleResultInfo = objArticleInfo.CreateArticleResultInfo(objLoadArticleContent, objLoadPictureCollection);

            if (objArticleResultInfo == null || objArticleResultInfo.ArticleInfo == null)
            {
                objInvokeResult.ResultCode = "ArticleResultInfoEmpty";
                objInvokeResult.ResultMessage = "文章不存在";
                return objInvokeResult;
            }

            bool objConvertTargetUrl = isConvertTargetUrl.HasValue && isConvertTargetUrl.Value == 1;
            if (objConvertTargetUrl)
            {
                objArticleResultInfo.ConvertTargetUrl(null, this.ArticlePlatformType);
            }

            if (isPictureThumbnail.HasValue && isPictureThumbnail.Value == 1)
            {
                if (objArticleResultInfo.PictureCollection.IsNoNull())
                {
                    foreach (ArticlePicture objArticlePicture in objArticleResultInfo.PictureCollection)
                    {
                        objArticlePicture.MaxPic = objArticlePicture.MaxPic.CreateThumbnailFromPicture();
                    }
                }
            }

            if (isContentThumbnail.HasValue && isContentThumbnail.Value == 1)
            {
                objArticleResultInfo.ArticleContent = objArticleResultInfo.ArticleContent.CreateThumbnailFromContent();
            }

            objInvokeResult.Data = objArticleResultInfo;

            return objInvokeResult;
        }

        /// <summary>
        /// 获取文章详情
        /// </summary>
        /// <param name="categoryId"></param>
        /// <param name="articleId"></param>
        /// <param name="publishDate"></param>
        /// <param name="isLoadArticleContent"></param>
        /// <param name="isLoadPictureCollection"></param>
        /// <param name="isConvertTargetUrl"></param>
        /// <param name="isPictureThumbnail"></param>
        /// <param name="isContentThumbnail"></param>
        /// <returns></returns>
        public virtual InvokeResult GetArticleCollect(int? parentId, string collectId, string publishDate, int? isLoadArticleContent = 0, int? isLoadPictureCollection = 0, int? isConvertTargetUrl = 1, int? isPictureThumbnail = 0, int? isContentThumbnail = 0)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (!parentId.HasValue)
            {
                objInvokeResult.ResultCode = "ParentIdEmpty";
                objInvokeResult.ResultMessage = "ParentId不存在";
                return objInvokeResult;
            }
            if (collectId.IsNull())
            {
                objInvokeResult.ResultCode = "CollectIdEmpty";
                objInvokeResult.ResultMessage = "CollectId不存在";
                return objInvokeResult;
            }
            if (publishDate.IsNull())
            {
                objInvokeResult.ResultCode = "publishDateEmpty";
                objInvokeResult.ResultMessage = "publishDate不存在";
                return objInvokeResult;
            }
            ArticleCollect objArticleCollect = this.GetArticleCollect(parentId.Value, collectId);
            if (objArticleCollect == null)
            {
                objInvokeResult.ResultCode = "ArticleCollectNull";
                objInvokeResult.ResultMessage = "文章不存在";
                return objInvokeResult;
            }
            if (!string.Equals(objArticleCollect.PublishDate.ToString("yyyyMMdd"), publishDate))
            {
                objInvokeResult.ResultCode = "PublishDateNotMatch";
                objInvokeResult.ResultMessage = "PublishDate不匹配";
                return objInvokeResult;
            }

            bool objLoadArticleContent = isLoadArticleContent.HasValue && isLoadArticleContent.Value == 1;
            bool objLoadPictureCollection = isLoadPictureCollection.HasValue && isLoadPictureCollection.Value == 1;
            ArticleResultInfo objArticleResultInfo = objArticleCollect.CreateArticleResultInfo(objLoadArticleContent, objLoadPictureCollection);

            if (objArticleResultInfo == null || objArticleResultInfo.ArticleInfo == null)
            {
                objInvokeResult.ResultCode = "ArticleResultInfoEmpty";
                objInvokeResult.ResultMessage = "文章不存在";
                return objInvokeResult;
            }

            bool objConvertTargetUrl = isConvertTargetUrl.HasValue && isConvertTargetUrl.Value == 1;
            if (objConvertTargetUrl)
            {
                objArticleResultInfo.ConvertTargetUrl(null, this.ArticlePlatformType);
            }

            if (isPictureThumbnail.HasValue && isPictureThumbnail.Value == 1)
            {
                if (objArticleResultInfo.PictureCollection.IsNoNull())
                {
                    foreach (ArticlePicture objArticlePicture in objArticleResultInfo.PictureCollection)
                    {
                        objArticlePicture.MaxPic = objArticlePicture.MaxPic.CreateThumbnailFromPicture();
                    }
                }
            }

            if (isContentThumbnail.HasValue && isContentThumbnail.Value == 1)
            {
                objArticleResultInfo.ArticleContent = objArticleResultInfo.ArticleContent.CreateThumbnailFromContent();
            }

            objInvokeResult.Data = objArticleResultInfo;

            return objInvokeResult;
        }

        /// <summary>
        /// 文章点击数+1
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="categoryId"></param>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public virtual InvokeResult ViewArticle(int? parentId, int? categoryId, int? articleId)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (!parentId.HasValue)
            {
                objInvokeResult.ResultCode = "ParentIdEmpty";
                objInvokeResult.ResultMessage = "ParentId不存在";
                return objInvokeResult;
            }
            if (!categoryId.HasValue)
            {
                objInvokeResult.ResultCode = "CategoryIdEmpty";
                objInvokeResult.ResultMessage = "CategoryId不存在";
                return objInvokeResult;
            }
            if (!articleId.HasValue)
            {
                objInvokeResult.ResultCode = "ArticleIdNull";
                objInvokeResult.ResultMessage = "对不起，文章编号不存在";
                return objInvokeResult;
            }

            objInvokeResult = HttpContext.Current.Request.Url.AbsoluteUri.CheckUrlUniqueSignature();
            if (objInvokeResult.ResultCode != "Success")
            {
                return objInvokeResult;
            }
            if (!this.ExistTableArticleRelatedAll(parentId.Value, categoryId.Value))
            {
                objInvokeResult.ResultCode = "TableNull";
                objInvokeResult.ResultMessage = "文章关联的表不存在";
                return objInvokeResult;
            }
            this.IncreaseViewCount(parentId.Value, categoryId.Value, articleId.Value);
            return objInvokeResult;
        }

        /// <summary>
        /// 文章点赞
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="categoryId"></param>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public virtual InvokeResult DiggArticle(int? parentId, int? categoryId, int? articleId)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (!parentId.HasValue)
            {
                objInvokeResult.ResultCode = "ParentIdEmpty";
                objInvokeResult.ResultMessage = "ParentId不存在";
                return objInvokeResult;
            }
            if (!categoryId.HasValue)
            {
                objInvokeResult.ResultCode = "CategoryIdEmpty";
                objInvokeResult.ResultMessage = "CategoryId不存在";
                return objInvokeResult;
            }
            if (!articleId.HasValue)
            {
                objInvokeResult.ResultCode = "ArticleIdNull";
                objInvokeResult.ResultMessage = "对不起，文章编号不存在";
                return objInvokeResult;
            }

            objInvokeResult = HttpContext.Current.Request.Url.AbsoluteUri.CheckUrlUniqueSignature();
            if (objInvokeResult.ResultCode != "Success")
            {
                return objInvokeResult;
            }
            if (!this.ExistTableArticleRelatedAll(parentId.Value, categoryId.Value))
            {
                objInvokeResult.ResultCode = "TableNull";
                objInvokeResult.ResultMessage = "文章关联的表不存在";
                return objInvokeResult;
            }

            string signature = HttpContext.Current.Request["signature"];
            var CurrentCache = TjZheng.Cache.CacheHelper.CacheInstance;
            string cacheKey = string.Format("LastDiggArticle_{0}_{1}", signature, articleId);
            //获取上一次点赞次数
            var objLastDigg = CurrentCache.GetCache(cacheKey).CreateValueDictionary();
            object obj = null;
            int diggCount = 0;
            if (objLastDigg.IsNoNull() && objLastDigg.TryGetValue("DiggCount", out obj))
            {
                diggCount = Convert.ToInt32(obj);
                if (objLastDigg.TryGetValue("DiggTime", out obj))
                {
                    DateTime diggTime = Convert.ToDateTime(obj);
                    if (diggCount >= 5 && diggTime.AddSeconds(5).IsGreaterThanCurrent())
                    {
                        objInvokeResult.ResultCode = "DiggArticleFrequently";
                        objInvokeResult.ResultMessage = "对不起，你点赞的频率过快";
                        return objInvokeResult;
                    }
                    diggCount++;
                    //设置上一次点赞缓存
                    CurrentCache.SetCache(cacheKey, new { DiggCount = diggCount, DiggTime = diggTime }, TimeSpan.FromSeconds(5));
                }
            }
            else
            {
                diggCount++;
                //设置上一次点赞缓存
                CurrentCache.SetCache(cacheKey, new { DiggCount = diggCount, DiggTime = DateTime.Now }, TimeSpan.FromSeconds(5));
            }

            this.IncreaseDiggCount(parentId.Value, categoryId.Value, articleId.Value);
            objInvokeResult.ResultMessage = "点赞成功";
            return objInvokeResult;
        }


        /// <summary>
        /// 文章点踩
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="categoryId"></param>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public virtual InvokeResult BuryArticle(int? parentId, int? categoryId, int? articleId)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (!parentId.HasValue)
            {
                objInvokeResult.ResultCode = "ParentIdEmpty";
                objInvokeResult.ResultMessage = "ParentId不存在";
                return objInvokeResult;
            }
            if (!categoryId.HasValue)
            {
                objInvokeResult.ResultCode = "CategoryIdEmpty";
                objInvokeResult.ResultMessage = "CategoryId不存在";
                return objInvokeResult;
            }
            if (!articleId.HasValue)
            {
                objInvokeResult.ResultCode = "ArticleIdNull";
                objInvokeResult.ResultMessage = "对不起，文章编号不存在";
                return objInvokeResult;
            }

            objInvokeResult = HttpContext.Current.Request.Url.AbsoluteUri.CheckUrlUniqueSignature();
            if (objInvokeResult.ResultCode != "Success")
            {
                return objInvokeResult;
            }
            if (!this.ExistTableArticleRelatedAll(parentId.Value, categoryId.Value))
            {
                objInvokeResult.ResultCode = "TableNull";
                objInvokeResult.ResultMessage = "文章关联的表不存在";
                return objInvokeResult;
            }

            string signature = HttpContext.Current.Request["signature"];
            var CurrentCache = TjZheng.Cache.CacheHelper.CacheInstance;
            string cacheKey = string.Format("LastBuryArticle_{0}_{1}", signature, articleId);
            //获取上一次点赞次数
            var objLastDigg = CurrentCache.GetCache(cacheKey).CreateValueDictionary();
            object obj = null;
            int buryCount = 0;
            if (objLastDigg.IsNoNull() && objLastDigg.TryGetValue("BuryCount", out obj))
            {
                buryCount = Convert.ToInt32(obj);
                if (objLastDigg.TryGetValue("BuryTime", out obj))
                {
                    DateTime buryTime = Convert.ToDateTime(obj);
                    if (buryCount >= 5 && buryTime.AddSeconds(5).IsGreaterThanCurrent())
                    {
                        objInvokeResult.ResultCode = "BuryArticleFrequently";
                        objInvokeResult.ResultMessage = "对不起，你点踩的频率过快";
                        return objInvokeResult;
                    }
                    buryCount++;
                    //设置上一次点赞缓存
                    CurrentCache.SetCache(cacheKey, new { BuryCount = buryCount, BuryTime = buryTime }, TimeSpan.FromSeconds(5));
                }
            }
            else
            {
                buryCount++;
                //设置上一次点赞缓存
                CurrentCache.SetCache(cacheKey, new { DiggCount = buryCount, BuryTime = DateTime.Now }, TimeSpan.FromSeconds(5));
            }

            this.IncreaseBuryCount(parentId.Value, categoryId.Value, articleId.Value);

            objInvokeResult.ResultMessage = "点踩成功";
            return objInvokeResult;
        }
    }
}
